package com.aperos.android15;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;

public class BarItems extends ItemizedOverlay<Bar> {

	private ArrayList<Bar> listBars = new ArrayList<Bar>();
	private Context mContext;
	
	public BarItems(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker)); 
		mContext = context;
	}

	@Override
	protected Bar createItem(int i) {
		return listBars.get(i);
	}
	

	@Override
	public int size() {
		return listBars.size();
	}
	
	public void addOverlay(Bar bar) {
		listBars.add(bar);
	    populate();
	}
	
	private static String pad(Integer c) {
		if (c == null) {
			return "00";
		} else if (c >= 10) {
	        return String.valueOf(c);
		} else {
	        return "0" + String.valueOf(c);
		}
	}
	
	@Override
	protected boolean onTap(int index) {
	  final Bar bar = listBars.get(index);
	  
	  LinearLayout layout = new LinearLayout(mContext);
	  layout.setOrientation(LinearLayout.VERTICAL);
	  
	  if (bar.getPriceBeer() != null) {
		  LinearLayout line = new LinearLayout(mContext);
		  line.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_price_beer = new TextView(mContext);
		  tv_lbl_price_beer.setText(mContext.getString(R.string.lbl_price_beer_dialog));
		  TextView tv_priceBeer = new TextView(mContext);
		  tv_priceBeer.setText(" " + bar.getPriceBeer().toString() + " " + mContext.getString(R.string.money));
		  line.addView(tv_lbl_price_beer);
		  line.addView(tv_priceBeer);
		  layout.addView(line);
	  }
	  
	  if (bar.getPriceCocktail() != null) {
		  LinearLayout line2 = new LinearLayout(mContext);
		  line2.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_price_cocktail = new TextView(mContext);
		  tv_lbl_price_cocktail.setText(mContext.getString(R.string.lbl_price_cocktail_dialog));
		  TextView tv_priceCocktail = new TextView(mContext);
		  tv_priceCocktail.setText(" " + bar.getPriceCocktail().toString() + " " + mContext.getString(R.string.money));
		  line2.addView(tv_lbl_price_cocktail);
		  line2.addView(tv_priceCocktail);
		  layout.addView(line2);
	  }
	  
	  if (bar.getHappyHourBeginHour() != null && bar.getHappyHourBeginMinute() != null && bar.getHappyHourEndHour() != null && bar.getHappyHourEndMinute() != null) {
		  LinearLayout line3 = new LinearLayout(mContext);
		  line3.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_happy_hour_begin = new TextView(mContext);
		  tv_lbl_happy_hour_begin.setText(mContext.getString(R.string.lbl_happy_hour_dialog));
		  TextView tv_happy_hour_begin  = new TextView(mContext);
		  int hour_begin = bar.getHappyHourBeginHour();
		  int minutes_begin = bar.getHappyHourBeginMinute();
		  String happyHourBegin = "";
		  happyHourBegin = pad(hour_begin) + ":" + pad(minutes_begin);
		  
		  int hour_end = bar.getHappyHourEndHour();
		  int minutes_end = bar.getHappyHourEndMinute();
		  String happyHourEnd = "";
		  happyHourEnd = pad(hour_end) + ":" + pad(minutes_end);
		  
		  tv_happy_hour_begin.setText(" " + happyHourBegin + " - " + happyHourEnd);
		  line3.addView(tv_lbl_happy_hour_begin);
		  line3.addView(tv_happy_hour_begin);
		  layout.addView(line3);
	  }
	  
	  if (bar.getPriceBeerHappyHour() != null) {
		  LinearLayout line5 = new LinearLayout(mContext);
		  line5.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_price_beer_happy_hour = new TextView(mContext);
		  tv_lbl_price_beer_happy_hour.setText(mContext.getString(R.string.lbl_price_beer_happy_hour_dialog));
		  TextView tv_price_beer_happy_hour  = new TextView(mContext);
		  tv_price_beer_happy_hour.setText(" " + bar.getPriceBeerHappyHour().toString() + " " + mContext.getString(R.string.money));
		  line5.addView(tv_lbl_price_beer_happy_hour);
		  line5.addView(tv_price_beer_happy_hour);
		  layout.addView(line5);
	  }
	  
	  if (bar.getPriceCocktailHappyHour() != null) {
		  LinearLayout line6 = new LinearLayout(mContext);
		  line6.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_price_cocktail_happy_hour = new TextView(mContext);
		  tv_lbl_price_cocktail_happy_hour.setText(mContext.getString(R.string.lbl_price_cocktail_happy_hour_dialog));
		  TextView tv_price_cocktail_happy_hour  = new TextView(mContext);
		  tv_price_cocktail_happy_hour.setText(" " + bar.getPriceCocktailHappyHour().toString() + " " + mContext.getString(R.string.money));
		  line6.addView(tv_lbl_price_cocktail_happy_hour);
		  line6.addView(tv_price_cocktail_happy_hour);
		  layout.addView(line6);
	  }
	
	  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	  dialog.setTitle(bar.getName());
	  dialog.setMessage(bar.getAddress()).setView(layout);
	 
	  dialog.setPositiveButton(mContext.getString(R.string.lbl_edit_bar_error_btn), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
	        	Intent intent = new Intent(mContext, BarErrorEdit.class);
	        	Service.currentBar = bar;
	        	
	        	mContext.startActivity(intent);
		    }
		});
	  
	  dialog.setNeutralButton(mContext.getString(R.string.lbl_edit_btn), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
	        	Intent intent = new Intent(mContext, BarEdit.class);
	        	intent.putExtra("edit", "edit");
	        	Service.currentBar = bar;
	        	
	        	mContext.startActivity(intent);
		    }
		});
	  dialog.setNegativeButton(mContext.getString(R.string.lbl_back_btn), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
	        	dialog.cancel();
		    }
		});
	  
	  if (bar.isInHappyHour()) {
		if (bar.getPriceBeerHappyHour() <= 3.5) {
			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_yellow_hh_36p));
  		} else if (bar.getPriceBeerHappyHour() <= 4.5) {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_orange_hh_36p));
  		} else {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_red_hh_36p));
  		}
	  } else {
  		if (bar.getPriceBeer() <= 3.5) {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_yellow_36p));
  		} else if (bar.getPriceBeer() <= 4.5) {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_orange_36p));
  		} else {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_red_36p));
  		}
	  }
	  
	  
	  dialog.show();
	  
	  
	  return true;
	}

}
